package com.ptswitch.material.service.impl;

import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.Resource;

import org.hibernate.Hibernate;
import org.springframework.stereotype.Service;

import com.hxzon.util.StringUtil;
import com.ptswitch.material.dao.MaterialDao;
import com.ptswitch.material.model.Material;
import com.ptswitch.material.service.MaterialService;
import com.ptswitch.material.util.DateUtils;
@Service("materialSer")
public class MaterialServiceImpl extends LoggerService implements MaterialService {
	@Resource
	private MaterialDao materialDao;
	
	public long countAll() {
		return materialDao.countAll();
	}

	public long countLike(Material model) {
		return materialDao.countLike(model);
	}
	
	public long countLike(String like){
		return materialDao.countLike(like);
	}

	public void delete(String id){
		materialDao.delete(id);
	}
	
	public void delete(Material model) {
		if(!model.getBomMaterials().isEmpty()){
			return ;
		}
//		uploadDao.deleteAll(model.getUploads());
		materialDao.delete(model);
	}
	
	public void deleteAll(){
		materialDao.deleteAll();
	}
	
	public void deleteAll(List<Material> models){
//		for(Materiel model:models){
//			Recorder.log(model);
//		}
		materialDao.deleteAll(models);
	}

	public Material get(String id) {
		Material materiel=materialDao.get(id);
		if(materiel!=null){
//			String hql="from BomMateriel as p where p.materiel.id='"+materiel.getId()+"' order by p.code";
//			materiel.setBomMateriels(bomMaterielDao.getAll(hql));
			Hibernate.initialize(materiel.getBomMaterials());
			Hibernate.initialize(materiel.getUploads());
		}
		return materiel;
	}
	
	public Material getByCode(String code){
		return materialDao.getByCode(code);
	}
	
	public List<Material> getByPcbDecal(String pcbDecal){
		return materialDao.getByPcbDecal(pcbDecal);
	}
	
	public boolean existPcbDecal(String pcbDecal){
		long count=materialDao.countList(" where p.pcbDecal='"+pcbDecal+"'");
//		logger.debug("{}",count);
		return count>0;
	}
	
	public List<Material> getAll(){
		return materialDao.getAll();
	}

	public List<Material> getAll(long first,long size) {
		return materialDao.getAll(first, size);
	}

	public List<Material> getLike(Material model,long first, long size) {
		return materialDao.getLike(model,first, size);
	}
	
	public List<Material> getLike(String like,long first,long size){
		return materialDao.getLike(like, first, size);
	}
	
	public List<Material> getList(String fieldName,String fieldValue,long first,long size){
		return materialDao.getList(fieldName, fieldValue,first,size);
	}
	
	public long countList(String fieldName,Object fieldValue){
		return materialDao.countList(fieldName, fieldValue);
	}

	public void save(Material model) {
		String state=model.getState() ;
		if (state== null || state.isEmpty()) {
			state = "一般";
		}
		if (state.equals("推荐") || state.equals("一般")
				|| state.equals("不推荐") || state.equals("停用") || state.equals("未发布")) {

		} else {
			state = state + "描述不规范，请检查";
		}
		model.setState(state);
		materialDao.save(model);
	}
	
	public void saveAll(List<Material> models){
		for(Material model:models){
			save(model);
		}
	}
	private Pattern p=Pattern.compile(".*-(\\d*)-.*");
	
	public String emptyCode(String code){
		code=StringUtil.escapeSql(code);
		List<Material> list = materialDao.getList(" where p.code like '"
				+ code + "-%' order by p.code", -1, -1);//code-%
		if(list.isEmpty()){
			return code+"-000-0";
		}
		String lastCode = list.get(list.size() - 1).getCode();
		System.out.println(lastCode);
		Matcher m = p.matcher(lastCode);
		try {
			m.matches();
			int index = Integer.parseInt(m.group(1)) + 1;
			return code +"-"+ DateUtils.format(index)+"-0";
		} catch (RuntimeException e) {
			e.printStackTrace();
		}
		return code;
	}
	
	public String valid(Material model){
		return null;
	}
	
}
